package com.ktv.web.controller.com.ktv.web.handler.impl;

import com.ktv.web.controller.com.ktv.web.handler.RequestHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * Created by ivey.yuan on 2016/3/1.
 */

@Component
public class AsyncRequestHandler extends AbstractRequestHandler implements RequestHandler {

    public String handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AsyncContext asyncContext = super.startAsyncRequest(request, response);
        StringBuilder sb = new StringBuilder();
        sb.append("{\"code\":200").append(",").append("\"date\":\"").append(new Date().toString()).append("\"}");
        super.writerResponse(response, sb.toString(),"application/json");
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        asyncContext.complete();
        return null;
    }
}
